package com.person.layui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Menu {
   private String id;
   private String parentId;
   private String title;
   private String icon;
   private String href;
   private boolean spread=false;
   private List<Menu>children;
   public  Menu(){}
   public Menu(String id, String parentId, String title, String icon, String href,
			boolean spread) {
		super();
		this.id = id;
		this.parentId = parentId;
		this.title = title;
		this.icon = icon;
		this.href = href;
		this.spread = spread;
 }
 public static Menu mapToMenu(Map<String,Object> map){
	 if(map==null) 
	 return null;
		return new Menu(
				map.get("id") == null ? null : map.get("id").toString(),
				map.get("pId") == null ? null : map.get("pId").toString(),
				map.get("name") == null ? "" : map.get("name").toString(),
				map.get("icon") == null ? null : map.get("icon").toString(),
				map.get("actionUrl") == null ? "" : map.get("actionUrl")
						.toString(), map.get("spread") == null ? false
						: Boolean.valueOf(map.get("spread").toString()));
	}
 
 public static List<Menu>listToMenus(String parentId,List<Map<String,Object>> list){
	 return cvtMenu(parentId,mapToMenuList(list));
 } 
 
 private static List<Menu> mapToMenuList(List<Map<String,Object>> list){
	 if(list==null||list.size()==0)
	 return null;
	 List<Menu> result=new ArrayList<Menu>();
	 for(Map<String,Object> map:list)
		 result.add(mapToMenu(map));
	 return result;
 }
  
 private static  List<Menu> cvtMenu(String parentId,List<Menu> list){
	 List<Menu> pMenus=new ArrayList<Menu>();
	 List<Integer> intList=new ArrayList<Integer>();
	 for(int i=0;i<list.size();i++){
		 if(list.get(i).getParentId().equals(parentId)){
			 pMenus.add(list.get(i));
			 intList.add(i);
		 }
	 }
  for(int i=0;i<intList.size();i++)
	     list.remove(intList.get(i));
   for(Menu m:pMenus)
	   findChildren(m,list);
   return pMenus;
 }
 
private static void findChildren(Menu m,List<Menu> list){
	 List<Integer> intList=new ArrayList<Integer>();
	 for(int i=0;i<list.size();i++){
		 if(list.get(i).getParentId().equals(m.getId())){
			 List<Menu> children=m.getChildren();
			 if(children==null)
				 children=new ArrayList<Menu>();
			 children.add(list.get(i));
			 m.setChildren(children);
			 intList.add(i);
		 }
	 }
    for(int i=0;i<intList.size();i++)
	     list.remove(intList.get(i));
    if(m.getChildren()!=null&&m.getChildren().size()>0)
    for(Menu temp:m.getChildren())
    	findChildren(temp,list);
}  
 
public String getId() {
	return id;
}
public void setId(String id) {
	this.id = id;
}
public String getParentId() {
	return parentId;
}
public void setParentId(String parentId) {
	this.parentId = parentId;
}
public String getTitle() {
	return title;
}
public void setTitle(String title) {
	this.title = title;
}
public String getIcon() {
	return icon;
}
public void setIcon(String icon) {
	this.icon = icon;
}
public String getHref() {
	return href;
}
public void setHref(String href) {
	this.href = href;
}
public boolean isSpread() {
	return spread;
}
public void setSpread(boolean spread) {
	this.spread = spread;
}
public List<Menu> getChildren() {
	return children;
}
public void setChildren(List<Menu> children) {
	this.children = children;
} 
 
}
